import config from '@/config'
import { onMounted, onUnmounted, ref, watch } from 'vue'
import {
  dicItemByCode,
  getJcHistoryData,
  getYSQProcessHistoryData,
  getYSQProcessRealtimeData,
  getYSQRgvRealtimeData,
  getYSQTabulateData, sameDayProductionLineCode
} from '@/api/mesApi'
import { useI18n } from 'vue-i18n'

let task = null

const killTask = () => {
  if (task) {
    clearInterval(task)
  }
}
const createData = async (remoteData, queryParams) => {
  const { processCode } = queryParams
  if (!queryParams.processCode) {
    remoteData.value.historyData = {}
    return
  }
  /* 4.养生区汇总数据 */
  getYSQTabulateData({ processCode }).then(function (res) {
    if (res) {
      for (const item of remoteData.value.processCount) {
        item.value = res[item.field] || 0
      }
    }
  }, function (e) {
    // console.error(e)
  })
  /* 1.单线别历史数据 */
  getYSQProcessHistoryData(queryParams).then(function (res) {
    if (res.statusCode === 300) {
      remoteData.value.historyData = {}
    } else {
      remoteData.value.historyData = { ...res }
    }
    queryParams.total = res.total
  }, function (e) {
    remoteData.value.historyData = {}
  })
  /* 2.养生区工序产出实时数据 */
  getYSQProcessRealtimeData({ processCode }).then(function (res) {
    Object.assign(remoteData.value.realTimeData, res)
  }, function (e) {
    // console.log(e)
  })

  /* 3.养生区RGV实时数据 */
  getYSQRgvRealtimeData({ processCode }).then(function (res) {
    // Object.assign(remoteData.value.realTimeData, res)
    remoteData.value.realTimeRGVData = res
  }, function (e) {
    console.log(e)
  })
  // /* 5.查询工序当天正在生产的产线 */
  // sameDayProductionLineCode({ processCode: 'JC' }).then(function (res) {
  //   remoteData.value.currentProductionLine = res
  //   console.log(res)
  //   // remoteData.value.currentProductionLine = [{ productionLineCode: 'JCPDLine1' }, { productionLineCode: 'JCPDLine3' }]
  // })
  /* 6.数据字典获取JC条线自动刷新的频率 */
  dicItemByCode({ code: 'YSQVIEW' }).then(function (res) {
    remoteData.value.refreshFrequency = res
    console.log(res)
  })
}
const dataReady = async (remoteData, once, queryParams) => {
  remoteData.value.ready = true
  remoteData.value.historyDataDgReady = false
  await createData(remoteData, queryParams)
  remoteData.value.historyDataDgReady = true
  if (!once && !task) {
    task = setInterval(async () => {
      await createData(remoteData, queryParams)
    }, 20000)
  }
}

export default function getKeepingData (queryParams, once) {
  const { t } = useI18n()
  const span = 4
  const remoteData = ref({
    ready: false,
    historyDataDgReady: false,
    historyData: {},
    realTimeData: {},
    realTimeRGVData: [],
    processCount: [
      {
        name: t('wms.healthAreasTime'),
        field: 'restQty',
        icon: config.ossUrl + '/afl-data-screen/images/icon-all-input.png',
        value: 0,
        span: span
      },
      {
        name: t('wms.healthAreaItems'),
        field: 'materialQty',
        icon: config.ossUrl + '/afl-data-screen/images/icon-output.png',
        value: 0,
        span: span,
        unit: 'plt'
      },
      {
        name: t('wms.heathToOccupy'),
        field: 'occupyQty',
        icon: config.ossUrl + '/afl-data-screen/images/icon-input.png',
        value: 0,
        span: span,
        unit: 'plt'
      },
      {
        name: t('wms.numberOfAvailable'),
        field: 'availableQty',
        icon: config.ossUrl + '/afl-data-screen/images/icon-takeOut.png',
        value: 0,
        span: span,
        unit: 'plt'
      }
    ],
    refreshFrequency: [], // 刷新频率
    currentProductionLine: [] // 当前生产工序产线
  })

  /* 挂在操作 */
  onMounted(() => dataReady(remoteData, once, queryParams))
  onUnmounted(killTask)
  /* 监听操作 */
  watch(() => queryParams, () => {
    // 状态设置为加载中
    dataReady(remoteData, true, queryParams)
  }, { deep: true })

  return { remoteData }
}
